/*
	Copyright 2008, 2009, 2010 Brightworks, Inc.
	
	This file is part of Language Mentor.
	
	Language Mentor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	Language Mentor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Language Mentor.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.langcollab.languagementor.util
{
    import com.langcollab.languagementor.constant.Constant_Misc;
    import com.brightworks.util.Debug;

    import flash.utils.Dictionary;

    import mx.collections.ArrayCollection;
    import mx.collections.Sort;
    import mx.collections.SortField;

    public class Utils_LangCollab
    {
        import flash.utils.describeType;

        public function Utils_LangCollab():void{}

        /**
         * param fileInfo A dictionary with one property for each chunk file in the module version,
         *                where the value for each property is the length of the file in milliseconds.
         * returns A string that is created by concatenating the last 3 digits of all file lengths.
         */
        public static function computeModuleVersionSignature(fileInfo:Dictionary):String {
            var result:String = "";
            var iFileCount:int = 0;
            var sFileId:String;
            var sFileLength:String;
            var sFileSignature:String;
        	// We can't count on the properties of a Dict always being accessed in same order, so we take the preliminary step
        	// of sorting the info by sFileId - this will ensure that a given module version will always have the same signature
            var tempArrayCollection:ArrayCollection = new ArrayCollection();
            var sort:Sort = new Sort();
            var sortField:SortField = new SortField("sFileId");
            sort.fields = [sortField];
            tempArrayCollection.sort = sort;
            tempArrayCollection.refresh();
            for (sFileId in fileInfo) {
                iFileCount++;
                if (iFileCount > Constant_Misc.MAX_ALLOWED_CHUNKS_PER_MODULE_VERSION) 
					Debug.error_Fatal(["Utils_LangCollab.computeModuleVersionSignature(): moduleVersion contains too many chunks", fileInfo]);/// throw error instead and handle by informing user - perhaps even catch before saving files
                sFileLength = fileInfo[sFileId];
                sFileSignature = sFileLength.substring(sFileLength.length - 3, sFileLength.length);
                tempArrayCollection.addItem(
                								{
                									sFileId			: sFileId,
                									sFileSignature	: sFileSignature
								                }
								            )
            }
            var oFileInfo:Object;
            for each (oFileInfo in tempArrayCollection) {
                result = result + oFileInfo.sFileSignature;
            }
            if (result.length == 0) 
				Debug.error_Fatal(["Utils_LangCollab.computeModuleVersionSignature(): fileInfo has no props", fileInfo]);/// throw error instead and handle by informing user - perhaps even catch before saving files
			// DLM 20091007 - The code below is a little research project that I ran to confirm the need for the sort step
			// implemented above - and the results confirmed that we can't count on the properties of a Dict always being
			// accessed in same order
			//
            /* var resultTest:String = "";
            var fileLength:int;
            for each (fileLength in fileInfo) {
                sFileLength = String(fileLength);
                sFileSignature = sFileLength.substring(sFileLength.length - 3, sFileLength.length);
                resultTest = resultTest + sFileSignature;
            }
            if (resultTest != result) Debug.warning(["Utils_LangCollab.computeModuleVersionSignature(): Good news! We've confirmed that can't count on the order in which a Dictionary is read. :)", result, resultTest]); */
            return result;
        }

        /*
            This method strips out any current screen state value in appStateCode and
            returns the remaining int. For example, if appStateCode = 333, this returns 330.
        */
        public static function getScreenCodeFromApplicationStateCode( appStateCode:int ):int
        {
            return appStateCode - (appStateCode % 10);
        }
    }
}